package spring.cloud.tasks.tasks_assign_manager.task_item_assign_event_handler;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.tasks_assign_manager.service.TaskItemAssignService;
import spring.cloud.tasks.tasks_assign_manager.task_item_assign_event.TaskExecutorOnlineOrOfflineTaskItemAssignEvent;
import spring.cloud.tasks.tasks_assign_manager.task_item_assign_event.TaskItemAssignEvent;

@Slf4j
@Setter
@Getter
public class TaskExecutorOnlineOrOfflineTaskItemAssignEventHandler extends TaskItemAssignEventHandler {


    private TaskItemAssignService taskItemAssignService;

    public TaskExecutorOnlineOrOfflineTaskItemAssignEventHandler(TaskItemAssignService taskItemAssignService) {

        this.taskItemAssignService = taskItemAssignService;
    }

    @Override
    public void handle(TaskItemAssignEvent taskItemAssignEvent) throws Exception {
        if (taskItemAssignEvent instanceof TaskExecutorOnlineOrOfflineTaskItemAssignEvent) {
            TaskExecutorOnlineOrOfflineTaskItemAssignEvent event = (TaskExecutorOnlineOrOfflineTaskItemAssignEvent) taskItemAssignEvent;
            String executorId = event.getExecutorId();
            String taskId = event.getTaskId();
            if (TaskExecutorOnlineOrOfflineTaskItemAssignEvent.type_task_executor_online.equals(event.getType())) {
                taskItemAssignService.taskExecutorOnlineAllotTask(taskId, executorId);
            } else if (TaskExecutorOnlineOrOfflineTaskItemAssignEvent.type_task_executor_offline.equals(event.getType())) {
                taskItemAssignService.taskExecutorOfflineAllotTask(taskId, executorId);
            } else {
            }
        }

    }


}
